<template>
  <div class="login container">
    <div class="form-group">
      <label for="">手机号</label>
      <input type="number" placeholder="验证手机号" maxlength="11" v-model="phone">
      <div class="btn" @click="sendCode" v-if="!sending">获取验证码</div>
      <div class="btn sending-btn" v-else>倒计时{{countDown}}S </div>
    </div>
    <div class="form-group">
      <label for="">验证码</label>
      <input type="text" name="" maxlength="10" placeholder="请输入验证码" v-model="phoneCode">
    </div>
    <button class="btn login-btn" open-type="getUserInfo" @getuserinfo="bindGetUserInfo" :disabled="disabled">提交</button>

    <!-- 弹窗 -->
    <div :class="['modal-wrap',modalShow ? 'show' : '']">
      <div class="modal-main">
        <div class="modal-text">授权小程序获取微信信息，才能获得更好体验</div>
        <button class="modal-btn" open-type="openSetting" @click="closeModal">去设置</button>
      </div>
    </div>
  </div>
</template>
<script>
import utils from "@/utils";
import wx from "wx";
import Api from "@/http/api";

export default {
  data() {
    return {
      phone: "",
      phoneCode: "",
      sending: false,
      countDown: 30,
      timer: null,
      submitData: { code: "" },
      modalShow: false,
      disabled: false
    };
  },
  methods: {
    async init() {
      wx.setNavigationBarTitle({ title: "验证手机号" });
      this.submitData.code = await this.getCode();
      utils.wxLogin();
    },
    async bindGetUserInfo(e) {
      if (e.mp.detail.errMsg === "getUserInfo:ok") {
        let res = e.mp.detail;
        this.submitData["encryptedData"] = res.encryptedData
          ? res.encryptedData
          : "";
        this.submitData["iv"] = res.iv ? res.iv : "";
        await this.submitForm();
      } else {
        this.modalShow = true;
      }
    },
    async sendCode() {
      if (this.sending) return;
      let reg = new RegExp(/^1[0-9]{10}$/);
      if (this.phone.length && reg.test(this.phone)) {
        this.sending = true;
        this.handleCountDown();
        Api.getCode(this.phone);
      } else {
        wx.showToast({
          title: "请正确输入手机号",
          duration: 2000,
          icon: "none"
        });
      }
    },
    handleCountDown() {
      this.timer && clearInterval(this.timer);
      this.timer = setInterval(() => {
        if (this.countDown === 0) {
          this.sending = false;
          this.countDown = 60;
          return;
        }
        this.countDown -= 1;
      }, 1000);
    },
    async submitForm() {
      let reg = new RegExp(/^1[0-9]{10}$/);
      if (!(this.phone.length && reg.test(this.phone))) {
        utils.showToast("请正确输入手机号");
        return;
      } else if (!this.phoneCode.length) {
        utils.showToast("请正确输入验证码");
        return;
      }
      this.disabled = true;
      this.submitData["phone"] = this.phone;
      this.submitData["phoneCode"] = this.phoneCode;
      let data = await Api.Login(this.submitData);
      if (data.errcode === 0) {
        data = data.data;
        data.token && wx.setStorageSync("token", data.token);
        data.member && wx.setStorageSync("member", data.member);
        data.userType && wx.setStorageSync("userType", data.userType);
        this.disabled = false;
        this.destoryData();
        if (data.firstLogin) {
          wx.redirectTo({ url: "codeUpload/main" });
        } else {
          wx.redirectTo({ url: "../index/main" });
        }
      }
      this.disabled = false;
    },
    closeModal() {
      this.modalShow = false;
    },
    getCode() {
      return new Promise((resolve, reject) => {
        wx.login({
          success: res => {
            resolve(res.code);
          }
        });
      });
    },
    destoryData() {
      clearInterval(this.timer);
      this.sending = false;
      this.countDown = 30;
    }
  },
  onLoad() {
    this.init();
  },
  onHide() {
    this.destoryData();
  }
};
</script>
<style lang="scss" scoped>
@import "styles/element.scss";
.login {
  @include comMargin;
  .form-group {
    @include flex-justify;
    height: 64rpx;
    padding: 22rpx 0;
    border-bottom: 1rpx solid #eee;
    label {
      width: 112rpx;
      font-size: 28rpx;
      color: #000;
    }
    input {
      display: block;
      flex: 1;
      height: 50rpx;
      // min-width: 0;
      font-size: 28rpx;
    }
    .btn {
      @include btn;
      width: 187rpx;
      height: 64rpx;
      line-height: 64rpx;
      font-size: 22rpx;
      &.sending-btn {
        background-color: transparent;
        color: #b2b2b2;
      }
    }
  }
  .login-btn {
    @include btn;
    margin-top: 92rpx;
  }
  .modal-text {
    padding: 45rpx 25rpx 18rpx;
    font-size: 30rpx;
    line-height: 1.35;
    font-size: 30rpx;
  }
  .modal-btn {
    @include btn;
    margin: 25rpx;
    font-size: 32rpx;
    height: 70rpx;
    line-height: 70rpx;
  }
}
</style>

